package com.jinglvhuanjing.app.utils;

import android.os.Handler;
import android.os.Message;

import com.example.x6.serial.SerialPort;
import com.jinglvhuanjing.app.bean.LoginEvent;
import com.jinglvhuanjing.app.bean.RunEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.InputStream;

public class SerialMsgHandler {

    public static String TAG = "SerialMsgHandler";

    private static SerialMsgHandler serialMsgHandler;

    private boolean start = false;

    private Thread thread;
    private Thread thread2;

    public SerialMsgHandler() {}

    public static final int CONNECT3 = 1;
    public static final int CONNECT2 = 2;

    public static SerialMsgHandler getInstance() {
        if (serialMsgHandler == null) {
            serialMsgHandler = new SerialMsgHandler();
        }
        return serialMsgHandler;
    }

    /**
     * 启动串口读取线程或停止
     */
    public void setStartListener(boolean start, Handler mHandler){
        this.start = start;
        if(start && thread==null) {
            // 当thread不为空时，外面再传进来的Handler无用
            thread = new Thread() {
                public void run() {
                    LOG.e(TAG, "读取数据:开始");
                    try {
                        // 获取SerialPort对象
                        SerialPort serialPort = SingletonSerl.getInstance().getSerialPortConnect3();
                        //从串口对象中获取输入流
                        InputStream inputStream = serialPort.getInputStream();
                        //使用循环读取数据，建议放到子线程去
                        while (true) {
                            Thread.sleep(200);
                            if (inputStream.available() > 0) {
                                //当接收到数据时，sleep 500
                                //sleep过后，再读取数据，基本上都是完整的数据
                                byte[] buffer = new byte[inputStream.available()];
                                int size = inputStream.read(buffer);// 写入数据
                                String hex = ByteUtil.hexBytesToString(buffer);
//                                //将来数据写入主线程
                                Message message = new Message();
                                message.obj = ByteUtil.hexBytesToString(buffer);
                                message.what = CONNECT2;
                                mHandler.sendMessage(message);
                                LOG.e(TAG, "读取数据(字符串):" + hex);

                                if(!StringUtils.isEmpty(hex)){
                                    LOG.e(TAG, "发送eventBus");
                                    RunEvent runEvent = new RunEvent();
                                    runEvent.setTAG(TAG);
                                    runEvent.setValue(buffer);
                                    EventBus.getDefault().post(runEvent);
                                }
                            }
                        }
                    } catch (Exception e) {
                        LOG.e(TAG, "读取数据(异常):" + e.toString());
                    }
                }
            };
            thread.start();
        }
    }

    /**
     * 启动串口读取线程或停止
     */
    public void setStartListenerLogin(Handler mHandler){
        if(thread2==null) {
            // 当thread不为空时，外面再传进来的Handler无用
            thread2 = new Thread() {
                public void run() {
                    LOG.e(TAG, "登录数据 读取数据:开始");
                    try {
                        // 获取SerialPort对象
                        SerialPort serialPort = SingletonSerl.getInstance().getSerialPortConnect2();
                        //从串口对象中获取输入流
                        InputStream inputStream = serialPort.getInputStream();
                        //使用循环读取数据，建议放到子线程去
                        while (true) {
                            Thread.sleep(200);
                            if (inputStream.available() > 0) {
                                //当接收到数据时，sleep 500
                                //sleep过后，再读取数据，基本上都是完整的数据
                                byte[] buffer = new byte[inputStream.available()];
                                int size = inputStream.read(buffer);// 写入数据
                                if(buffer!=null && buffer.length>0){
                                    String hex = ByteUtil.hexBytesToString(buffer);
                                    //将来数据写入主线程
                                    Message message = new Message();
                                    message.obj = ByteUtil.hexBytesToString(buffer);
                                    message.what = CONNECT3;
                                    mHandler.sendMessage(message);
                                    LOG.e(TAG, "登录数据 读取数据(字符串):" + hex);

                                    if(!StringUtils.isEmpty(hex)){
                                        LOG.e(TAG, "登录发送eventBus");
                                        LoginEvent loginEvent = new LoginEvent();
                                        loginEvent.setTAG(TAG);
                                        loginEvent.setValue(hex);
                                        EventBus.getDefault().post(loginEvent);
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        LOG.e(TAG, "登录数据 读取数据(异常):" + e.toString());
                    }
                }
            };
            thread2.start();
        }
    }

//    /*
//     * 启动串口读取线程与回调
//     */
//    public void startCallback() {
//        Handler mHandler = new Handler() {
//            @Override
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//                switch (msg.what) {
//                    case 0:
//                        //do something,refresh UI;
//                        LOG.e(TAG, "读取内容:" + msg.obj);
//                        break;
//                    default:
//                        break;
//                }
//            }
//        };
//    }
}
